import initCalendar, {
    getSelectedDay,
    setTodoLabels,
    disableDay,
    switchView,
    jump,
  // enableArea
    } from '../../template/calendar/index';
const app = getApp()
import BaseModel from '../../utils/common.js'
var myDate = new Date();//获取系统当前时间
var current_year = myDate.getYear(); //获取当前年
var current_mouth = myDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
var current_date = myDate.getDate(); //获取当前日(1-31)
var current_day = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
var current_year = myDate.getFullYear();
var current_dateAll = current_year+'-'+current_mouth+'-'+current_day; //获取当前日期   年月日
/*if(current_mouth<10){  //月  小于10前面加0
  current_mouth="0"+current_mouth
}
if(current_date<10){ //日  小于10前面加0
  current_date="0"+current_date
}*/
if(current_day==0){  //周几
  current_day="日"
}else if(current_day==1){
  current_day="一"
}else if(current_day==2){
  current_day="二"
}else if(current_day==3){
  current_day="三"
}else if(current_day==4){
  current_day="四"
}else if(current_day==5){
  current_day="五"
}else if(current_day==6){
  current_day="六"
}
/*手机触摸滑动*/
let touchDotX = 0;//X按下时坐标
let touchDotY = 0;//y按下时坐标
let interval;//计时器
let time = 0;//从按下到松开共多少时间*100
Page({
  data:{
    week_mouth:1, //周月切换
    roleCode:"",//角色  1 分总  2员工 3财务
    dataList:[],//获取到的数据
    page:1,
    size:6, //每页条数
    code:1,//导航标识  默认公司日程
    dayStyle: [
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#0097ff' },
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#0097ff' }
    ],
    current_dateAll:current_dateAll,//年月日
    current_mouth:current_mouth,//月
    current_date:current_date,//日
    current_day:current_day,//周几
    eventMesData:[],//事件标记数据
    footer_more:"", //无数据
    empty_title:"",
    empty_img:"",
    empty_again:"",//重试
    is_clickTab:0, //重试或下拉是0，上啦是1
    is_onPageScroll:false,//页面是否滚动

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      roleCode:wx.getStorageSync('roleCode')
    })
    var current_dateAll = myDate.toLocaleDateString(); //获取当前日期   年月日
    this.setData({
      current_dateAll: current_dateAll
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getEventMesData();//获取事件标记
    this.getCustomerMesData();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    /*this.getCustomerMesData();
    this.getEventMesData();//获取事件标记*/
    initCalendar({
      afterTapDay: (currentSelect, allSelectedDays) => {
        let clickMonth = currentSelect.month;  //点击选中 月
        let clickDay = currentSelect.day; //点击选中 日
        var that = this;
        /*if(clickMonth.toString().length==1){  //月  小于10前面加0
          clickMonth="0"+clickMonth
        }
        if(clickDay.toString().length==1){ //日  小于10前面加0
          clickDay="0"+clickDay
        }*/
        var current_dateAll =currentSelect.year+'-'+currentSelect.month+'-'+currentSelect.day  //当前点击的年月日
        //根据年月日计算周几
        var arys1 = current_dateAll.split('-');   //日期为输入日期，格式为 2013-3-10
        var ssdate = new Date(arys1[0], parseInt(arys1[1] - 1), arys1[2]);
        var week=String(ssdate.getDay()).replace("0","日").replace("1","一").replace("2","二").replace("3","三").replace("4","四").replace("5","五").replace("6","六")//就是你要的星期几
        that.setData({
          current_mouth:clickMonth,
          current_date:clickDay,
          current_day:week, //周几
          current_dateAll:current_dateAll,
          dataList:[],
          page:1
        })
        that.getCustomerMesData(); //点击获取数据
      },
      whenChangeMonth(current, next) {
        //改变月
        // console.log(current);
        // console.log(next);
      },
      /**
       * 日历初次渲染完成后触发事件，如设置事件标记
       */
      afterCalendarRender(ctx) {
        //disableDay(1);
        // enableArea(['2018-10-7', '2018-10-28']);
      }
    });
    /*switchView('week');*/


  },
  onPageScroll:function(e){
    console.log(e)
/*
    this.setData({
      is_onPageScroll:true

    })*/
    /*switchView('week');*/
  },
  //获取事件标记数据
  getEventMesModel:new BaseModel({
    url: "allDiaryDate",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getEventMesData(){
    this.getEventMesModel
        .fetch({})
        .then(res => {
          this.setData({
            eventMesData:res.data
          })
          /*var data = [{
            day:"6",
            month:"4",
            year:"2019"
          },{
            day:"7",
            month:"4",
            year:"2019"
          }]*/
          setTodoLabels({
            pos: 'bottom',
            dotColor: '#f13d3d',
            circle: false,
            days: res.data//获取事件标记
          });
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },

  /*// 触摸开始事件
  touchStart: function(e) {
  touchDotX = e.touches[0].pageX; // 获取触摸时的原点
  touchDotY = e.touches[0].pageY;
  // 使用js计时器记录时间
  interval = setInterval(function() {
    time++;
  }, 1000);
},
  // 触摸结束事件
  touchEnd: function(e) {
  let touchMoveX = e.changedTouches[0].pageX;
  let touchMoveY = e.changedTouches[0].pageY;
  let tmX = touchMoveX - touchDotX;
  let tmY = touchMoveY - touchDotY;
  if (time < 20) {
    let absX = Math.abs(tmX);
    let absY = Math.abs(tmY);
    if (absY > absX * 2 && tmY<0) {
      console.log("上滑动")
      switchView('week');
      this.setData({
        is_onPageScroll:true
      })
    }else{
     console.log("下滑动")
     switchView();
      this.setData({
        is_onPageScroll:false
      })
    }
  }
  clearInterval(interval); // 清除setInterval
  time = 0;
},*/
  //点击周月切换
  /*switchView() {
    if (!this.weekMode) {
      switchView('week');
      this.setData({
        week_mouth:0
      })
    } else {
      switchView();
      this.setData({
        week_mouth:1
      })
    }
  },*/
  preventTouchMove:function(){

  },
  /*回到今天*/
  back_today:function(){
    var current_dateAll = myDate.toLocaleDateString(); //获取当前日期   年月日
    this.setData({
      current_dateAll:current_dateAll,
      page:1,
      dataList:[],
      current_mouth: current_mouth,//月
      current_date: current_date,//日
      current_day: current_day//周几
    })

    jump(current_year, current_mouth, current_date); // 跳转至今天
    this.data.dataList=[],
    this.getCustomerMesData(); //获取数据
  },
  /*日程tab切换*/
  clickNav:function(e){
    this.setData({
      code:e.currentTarget.dataset.id,
      dataList: [],
      is_clickTab:0,
      page:1
    })
    this.getCustomerMesData(); //获取数据
  },
  //获取数据
  getCustomerMesModel:new BaseModel({
    url: "getDiary",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getCustomerMesData(){
    this.getCustomerMesModel
        .fetch({
          type_id:this.data.code,
          page:this.data.page,
          size:this.data.size,
          date: this.data.current_dateAll
        })
        .then(res => {
          if(this.data.is_clickTab==0){
            this.setData({
              dataList:res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"",
              })
            }else{
              //成功
              var dataList_list = this.data.dataList;
              for(var i=0;i<res.data.length;i++){
                dataList_list.push(res.data[i])
              }
              this.setData({
                dataList:dataList_list
              })
            }
          }
        })
        .catch(res => {
          //失败
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
  //重试
  again_request:function(){
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getEventMesData();//获取事件标记
    this.getCustomerMesData();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getCustomerMesData();
    wx.stopPullDownRefresh(); //停止下拉
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    this.setData({
      is_clickTab:1
    })
    this.getCustomerMesData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})
